package com.xunyin.modules.mtstore.service.mapper;


import com.xunyin.common.mapper.CoreMapper;
import com.xunyin.modules.mtstore.bean.StoreDistanceBean;
import com.xunyin.modules.mtstore.domain.MtStore;
import com.xunyin.modules.mtstore.vo.MtStoreNameVo;
import com.xunyin.modules.mtstore.vo.MtStoreVo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 门店表 Mapper 接口
 * <p>
 * Created by xunyinyunshang <3594419785@qq.com>
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@Repository
public interface MtStoreMapper extends CoreMapper<MtStore> {

    @Select("select * from mt_store t where t.NAME = #{name} ")
    MtStore queryStoreByName(@Param("name") String name);

    @Update("update mt_store p set p.IS_DEFAULT = 'N' ")
    void resetDefaultStore();

    @Select("<script>select * from mt_store where id in <foreach collection=\"ids\" index=\"index\" item=\"id\" open=\"(\" separator=\",\" close=\")\">#{id}</foreach></script>")
    List<MtStore> findStoresByIds(@Param("ids") List<Integer> ids);

    @Select("SELECT t.id,(6371 * ACOS(COS( RADIANS(#{latitude}))*COS(RADIANS(t.latitude))*COS(RADIANS(t.longitude ) - RADIANS(#{longitude})) + SIN(RADIANS(#{latitude}))*SIN(RADIANS(t.latitude)))) AS distance" +
            "FROM mt_store t WHERE t.status = 'A'" +
            "<if test=\"keyword != null and keyword != ''\">" +
            "AND t.name LIKE concat('%',#{keyword},'%')" +
            "</if>" +
            "ORDER BY distance LIMIT 0,1000</script>")
    List<StoreDistanceBean> queryByDistance(@Param("keyword") String keyword, @Param("latitude") String latitude, @Param("longitude") String longitude);


    @Select("SELECT st.id as id,st.id as storeId,st.name,st.hours,st.phone, " +
            " st.longitude,st.latitude,st.address,st.image,  " +
            " st.address,st.city_code as cityCode,st.instr," +
            " st.status,st.start_time_num AS startTimeNum,  " +
            " (6371 * ACOS(COS( RADIANS(#{latitude}))*COS(RADIANS(st.latitude))*COS(RADIANS(st.longitude ) - RADIANS(#{longitude})) + SIN(RADIANS(#{latitude}))*SIN(RADIANS(st.latitude)))) AS distance" +
            " FROM mt_store st " +
            " WHERE st.city_code = #{cityCode} AND st.status = 'A' and st.is_del=0 ORDER BY distance LIMIT 0,1000 ")
    @Results(
            @Result(
                    property = "storeRoomList", column = "storeId",
                    javaType = List.class,
                    many = @Many(select = "com.xunyin.modules.mtstore.service.mapper.MtStoreRoomMapper.getStoreRoomList")
            )
    )
    List<MtStoreVo> getStoreList(@Param("cityCode") String cityCode, @Param("latitude") String latitude, @Param("longitude") String longitude);

    @Select("SELECT st.id as id,st.id as storeId,st.name,st.hours,st.phone, " +
            " st.longitude,st.latitude,st.address,st.image,  " +
            " st.address,st.city_code as cityCode,st.instr," +
            " st.status,st.start_time_num AS startTimeNum,  " +
            " (6371 * ACOS(COS( RADIANS(#{latitude}))*COS(RADIANS(st.latitude))*COS(RADIANS(st.longitude ) - RADIANS(#{longitude})) + SIN(RADIANS(#{latitude}))*SIN(RADIANS(st.latitude)))) AS distance" +
            " FROM mt_store st " +
            " WHERE st.status = 'A' and st.is_del=0 ORDER BY distance LIMIT 0,1000 ")
    @Results(
            @Result(
                    property = "storeRoomList", column = "storeId",
                    javaType = List.class,
                    many = @Many(select = "com.xunyin.modules.mtstore.service.mapper.MtStoreRoomMapper.getStoreRoomList")
            )
    )
    List<MtStoreVo> getStoreListV2( @Param("latitude") String latitude, @Param("longitude") String longitude);

    @Select("SELECT st.id as storeId,st.name as storeName from mt_store st where st.status = 'A' and st.is_del=0 ")
    List<MtStoreNameVo> getStoreNameList();

}
